package com.acwing.partition4;

import java.io.*;
import java.util.PriorityQueue;

/**
 * @author `RKC`
 * @date 2022/1/15 15:44
 */
public class AC383观光 {

    private static final int N = 1010, M = 10010, INF = 0x3f3f3f3f;
    private static int[] head = new int[N], element = new int[M], next = new int[M], weight = new int[M];
    private static int[][] dist = new int[N][2], cnt = new int[N][2];
    private static boolean[][] visited = new boolean[N][2];

    private static int n = 0, m = 0, index = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int k = Integer.parseInt(reader.readLine());
        while (k-- > 0) {
            String[] s = reader.readLine().split("\\s+");
            n = Integer.parseInt(s[0]);
            m = Integer.parseInt(s[1]);
            reset();
            for (int i = 0; i < m; i++) {
                s = reader.readLine().split("\\s+");
                int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), w = Integer.parseInt(s[2]);
                createEdge(u, v, w);
            }
            s = reader.readLine().split("\\s+");
            int start = Integer.parseInt(s[0]), end = Integer.parseInt(s[1]);
            writer.write(dijkstra(start, end) + "\n");
        }
        writer.flush();
    }

    private static int dijkstra(int start, int end) {
        //三元组 (点编号, 类型 0表示最短路 1表示次短路, 距离)
        PriorityQueue<int[]> heap = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[2], o2[2]));
        heap.add(new int[]{start, 0, 0});
        cnt[start][0] = 1;
        while (!heap.isEmpty()) {
            int[] tuple = heap.poll();
            int u = tuple[0], type = tuple[1], du = tuple[2];
            if (visited[u][type]) continue;
            visited[u][type] = true;
            //当前节点更新其它节点
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i], dv = du + weight[i];
                //如果当前能更新v的最短路，将v当前最短路赋值给次短路，v的最短路使用新值，同时更新方案数
                if (dist[v][0] > dv) {
                    dist[v][1] = dist[v][0];
                    cnt[v][1] = cnt[v][0];
                    heap.add(new int[]{v, 1, dist[v][1]});
                    dist[v][0] = dv;
                    cnt[v][0] = cnt[u][type];
                    heap.add(new int[]{v, 0, dist[v][0]});
                } else if (dist[v][0] == dv) {
                    cnt[v][0] += cnt[u][type];
                } else if (dist[v][1] > dv) {
                    //如果只能更新v的次短路，就更新v的次短路，并记录方案数
                    dist[v][1] = dv;
                    cnt[v][1] = cnt[u][type];
                    heap.add(new int[]{v, 1, dist[v][1]});
                } else if (dist[v][1] == dv) {
                    cnt[v][1] += cnt[u][type];
                }
            }
        }
        int answer = cnt[end][0];
        if (dist[end][0] + 1 == dist[end][1]) answer += cnt[end][1];
        return answer;
    }

    private static void reset() {
        index = 0;
        for (int i = 0; i < N; i++) {
            head[i] = -1;
            dist[i][0] = dist[i][1] = INF;
            cnt[i][0] = cnt[i][1] = 0;
            visited[i][0] = visited[i][1] = false;
        }
    }

    private static void createEdge(int u, int v, int w) {
        element[index] = v;
        next[index] = head[u];
        head[u] = index;
        weight[index++] = w;
    }
}
